let express=require("express")();
let mysql=require("mysql");

// Node解决跨域问题
express.all("/*", function(req, res, next) {
    // 跨域处理
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next(); // 执行下一个路由
})


//数据库连接
let sql=mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"123456",
    database:"userinfo"
})

//尝试数据库连接
sql.connect()

//发送路由请求
//登录
express.get("/deng",(request,response)=>{
    let username=request.query.username;
    let userpass=request.query.userpass;

    sql.query(`SELECT * FROM vip WHERE username="${username}" AND userpass="${userpass}"`,(error,data)=>{
        if(error){
            response.send("error")
        }
        else{
            if(data.length===0){
                response.send("error")
            }
            else{
            response.send("success")
           
        }
        }
       
    })
})

//注册
express.get("/cha",(request,response)=>{
    let username=request.query.username;
    let tel=request.query.tel;
    let userpass=request.query.userpass;
    sql.query(`INSERT INTO vip (username,userpass,tel) VALUES ("${username}","${userpass}","${tel}")`,(error)=>{
        if(error){
            response.send("error")     
        }
        else{
            response.send("success")   
        }
    })
})
//添加商品信息
express.get("/addpro",(request,response)=>{
    let proname=request.query.proname;
    let price=request.query.price;
    let count=request.query.count;
    let proimg=request.query.proimg;
    sql.query(`INSERT INTO goodsinfo (proname,price,count,proimg) VALUES ("${proname}","${price}","${count}","${proimg}")`,(error)=>{
        if(error){
            response.send("error")     
        }
        else{
            response.send("success")   
        }
    })
})
//=====双表查询=====================
express.get("/countgoods",function (request,response){
	let id = request.query.id;
	sql.query(`select * from goodsnum join  goodlist on goodsnum.id=goodlist.goodsid where goodsnum.id=${id}`,function(error,data){
		//如果有错误
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.send(JSON.stringify(data));
	})
})
//下架商品信息
express.get("/deletepro",(request,response)=>{
    const proid = request.query.proid;
    sql.query(`DELETE FROM product WHERE proid="${proid}"`,(error,data)=>{
        if(error){
            response.send("error")     
        }
        else{
            response.send("success")   
        }
    })
})

//编辑商品信息
express.get("/changepro",(request,response)=>{
    let proname=request.query.proname;
    let price=request.query.price;
    let num=request.query.num;
    let proid=request.query.proid;
    let downtime=request.query.downtime;
    sql.query(`UPDATE product SET proname="${proname}",price="${price}",num="${num}",downtime="${downtime}" WHERE proid="${proid}"`,(error)=>{
        
        if(error){
            response.send("error")     
        }
        else{
            response.send("success")   
        }
    })
})
//个人信息列表
express.get("/myinfo",function (request,response){
	let goodsimg = request.query.limg;
	let goodname = request.query.newgoodname;
	let money = request.query.newnewmoney;
	let data = request.query.newdate;
	let gouname = request.query.gindname;
	
	sql.query(`insert into goumai(goodsimg,goodname,money,data,gouname) values ("${goodsimg}","${goodname}","${money}","${data}","${gouname}")`,(error,result)=>{
		//如果有错误
		if (error) {
			console.log(error);
			response.end("error");
			return;
		}
		response.end("success");
	})
})
//获取商品信息
express.get("/getproduct",(request,response)=>{
	const proid = request.query.proid;
	let s = proid ?`SELECT * FROM goodsinfo WHERE proid="${proid}"`: `SELECT * FROM goodsinfo ORDER BY proid`;
	sql.query(s,(error,data)=>{
		if(error){
			response.end("error")
		}
		else{
			response.send(JSON.stringify(data))
		}
	})
})
//购物车信息
express.get("/insertbuycar",function (request,response){
	let goodsimg = request.query.proimg;
	let goodname = request.query.newgoodname;
	let money = request.query.shuju;
	sql.query(`insert  into gouwuchelist(tupian,foodname,danjia) values ("${goodsimg}","${goodname}","${money}") `,function(error,data){
		//如果有错误
			if (error) {
				console.log(error);
				response.end("error");
				return;
			}
			response.end("success");
	})
})
//查询购物车
express.get("/selectbuycar",function (request,response){
	sql.query(`select * from gouwuchelist `,function(error,data){
		//如果有错误
			if(error){
				console.log(error);
				response.end("error");
				return;
			}
			response.send(JSON.stringify(data));
		})
})
//删除购物信息
express.get("/usersgoodsid",function (request,response){
	let Id = request.query.id;
	sql.query(`delete from gouwuchelist where Id=${Id} `,function(error,data){
		//如果有错误
			if(error){
				console.log(error);
				response.end("error");
				return;
			}
			response.end("success");
		})
})
//订单信息
express.get("/dingdan",function (request,response){
	let username = request.query.username;
	let photo = request.query.photo;
	let youbian = request.query.youbian;
	let threecity = request.query.threecity;
	let hoset = request.query.hoset;
	let shuliang = request.query.shuliang;
	let huafei = request.query.huafei;
	let newstuname = request.query.newstuname;
	sql.query(`insert into photostu(username,photo,youbian,threecity,hoset,shuliang,huafei,gouname) values ("${username}","${photo}","${youbian}","${threecity}","${hoset}","${shuliang}","${huafei}","${newstuname}")`,(error,result)=>{
		//如果有错误
		if (error) {
			console.log(error);
			response.end("error");
			return;
		}
		response.end("success");
	})
})
express.listen(8080)
console.log("当前运行的是8080端口")